package org.bsplus.client.mvp;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;
import java.util.List;
import org.bsplus.rpc.dto.IssueListItemDTO;

public class ListIssueView extends ContentPanel implements ListIssuePresenter.Display
{
    private static final int RESULTS_PER_PAGE = 10;

    public ListIssueView()
    {
        setFrame(true);
        setScrollMode(Scroll.AUTO);
        setHeading("List Issues");
        setLayoutOnChange(true);
    }

    @Override
    public Widget asWidget()
    {
        return this;
    }

    @Override
    public void startProcessing()
    {
    }

    @Override
    public void stopProcessing()
    {
    }

    @Override
    public void init(RpcProxy<PagingLoadResult<IssueListItemDTO>> proxy)
    {
        final PagingLoader<PagingLoadResult<IssueListItemDTO>> loader = new BasePagingLoader<PagingLoadResult<IssueListItemDTO>>(
                proxy);
        loader.setRemoteSort(true);

        ListStore<IssueListItemDTO> store = new ListStore<IssueListItemDTO>(loader);

        final PagingToolBar toolBar = new PagingToolBar(RESULTS_PER_PAGE);
        toolBar.bind(loader);

        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
        columns.add(new ColumnConfig(IssueListItemDTO.DISPLAY_ISSUE_NUMBER, "Issue Number", 80));
        columns.add(new ColumnConfig(IssueListItemDTO.SUMMARY, "Summary", 300));
        columns.add(new ColumnConfig(IssueListItemDTO.STATUS, "Status", 60));
        columns.add(new ColumnConfig(IssueListItemDTO.ASSIGNEE, "Assignee", 100));
        columns.add(new ColumnConfig(IssueListItemDTO.SEVERITY, "Severity", 100));
        columns.add(new ColumnConfig(IssueListItemDTO.TYPE, "Type", 70));

        ColumnModel cm = new ColumnModel(columns);

        final Grid<IssueListItemDTO> grid = new Grid<IssueListItemDTO>(store, cm);
        grid.addListener(Events.Attach, new Listener<GridEvent<IssueListItemDTO>>()
        {
            @Override
            public void handleEvent(GridEvent<IssueListItemDTO> be)
            {
                PagingLoadConfig config = new BasePagingLoadConfig();
                config.setOffset(0);
                config.setLimit(RESULTS_PER_PAGE);
                loader.load(config);
            }
        });
        grid.setLoadMask(true);
        grid.setBorders(true);
        grid.setHeight(300);
        grid.setWidth(800);
        removeAll();
        add(grid);
        add(toolBar);
    }
}
